<template>
  <a-input v-bind="$attrs" :class="prefixCls" :size="size" :value="state">
    <template #addonAfter>
      <CountButton
        :size="size"
        :count="count"
        :value="state"
        :toUser="toUser"
        :beforeStartFunc="sendCodeApi"
        :bizType="bizType"
        :taskCode="taskCode"
        @submit-verify-info="submitVerifyInfo"
        :enableManMachineVerify="enableManMachineVerify"
      />
    </template>
    <template #[item]="data" v-for="item in Object.keys($slots).filter((k) => k !== 'addonAfter')">
      <slot :name="item" v-bind="data || {}"></slot>
    </template>
  </a-input>
</template>
<script lang="ts">
  import { defineComponent, PropType } from "vue"
  import CountButton from "./CountButton.vue"
  import { useDesign } from "/@/hooks/web/useDesign"
  import { useRuleFormItem } from "/@/hooks/component/useFormItem"

  const props = {
    value: { type: String },
    toUser: { type: String },
    verifyInfo: { type: Object, default: {} },
    size: { type: String, validator: (v) => ["default", "large", "small"].includes(v) },
    count: { type: Number, default: 60 },
    bizType: { type: String },
    taskCode: { type: String },
    // 是否开启人机校验
    enableManMachineVerify: { type: Boolean, default: false },
    sendCodeApi: {
      type: Function as PropType<(arg?: Recordable) => Promise<boolean | undefined>>,
      default: null,
    }
  }

  export default defineComponent({
    name: "CountDownInput",
    components: { CountButton },
    inheritAttrs: false,
    props,
    emits: ["update:verifyInfo"],
    setup(props, { emit }) {
      const { prefixCls } = useDesign("countdown-input")
      const [state] = useRuleFormItem(props)

      const submitVerifyInfo = (info) => emit("update:verifyInfo", info)
      return { prefixCls, state, submitVerifyInfo }
    },
  })
</script>
<style lang="less">
  @prefix-cls: ~"@{namespace}-countdown-input";

  .@{prefix-cls} {
    .ant-input-group-addon {
      padding-right: 0;
      background-color: transparent;
      border: none;

      button {
        font-size: 14px;
      }
    }
  }
</style>
